package com.gs.synedocket.model;


import android.util.Log;

import com.gs.synedocket.apiresult.ApiResultData;
import com.gs.synedocket.apiresult.PagedList;
import com.gs.synedocket.callback.Callback;
import com.gs.synedocket.constant.AllConstants;
import com.gs.synedocket.constant.DbMsg;
import com.gs.synedocket.db.dto.DocketDto;
import com.gs.synedocket.db.dto.DocketSummaryDto;
import com.gs.synedocket.db.dto.ScanPaperDto;
import com.gs.synedocket.db.query.DocketQuery;
import com.gs.synedocket.db.sqlite.DBApi;
import com.gs.synedocket.model.listener.OnResultListener;
import com.gs.synedocket.util.GsonUtil;
import com.gs.synedocket.util.OKHttpUtil;
import com.gs.synedocket.util.StringUtil;
import com.gs.synedocket.util.ThreadManager;

import java.lang.reflect.Type;
import java.util.List;

public class DocketModel {
    public void getOn(String docketId, String truckNo, final OnResultListener onResultListener, Type typeOfT) {
//        OKHttpUtil.getInstance().getAsync(OKHttpUtil.NET_URL + "/api/Dockets/onhand?docketId="+docketId+"&truckNo="+truckNo, new Callback() {
//            @Override
//            public void onSuccess(Object obj) {
//                onResultListener.onSuccess(obj);
//            }
//
//            @Override
//            public void onFailed(Exception e) {
//                onResultListener.onFails(e);
//            }
//        }, typeOfT);
        ThreadManager.getInstance().execute(() -> {
            DocketDto docketDto = DBApi.getInstance().queryDocketOnHand(docketId, truckNo);
            ApiResultData<Object> result = new ApiResultData<>();
            result.setStatus(2000);
            result.setMessage(docketDto == null ? DbMsg.EMPTY_DOCKET : DbMsg.QUERY_SUCCESS);
            result.setData(docketDto);
            OKHttpUtil.getInstance().getHandler().post(() -> {
                onResultListener.onSuccess(result);
                Log.i(AllConstants.TAG, GsonUtil.getInstance().toJson(result));
            });
        });
    }

    public void logDetail(String docketId, final OnResultListener onResultListener, Type typeOfT) {
        OKHttpUtil.getInstance().getAsync(OKHttpUtil.NET_URL + "/api/dockets/deliveryLogs?docketId="+docketId, new Callback() {
            @Override
            public void onSuccess(Object obj) {
                onResultListener.onSuccess(obj);
            }

            @Override
            public void onFailed(Exception e) {
                logDetailLocal(docketId, onResultListener, typeOfT);
//                onResultListener.onFails(e);
            }
        }, typeOfT);
    }

    public void logDetailLocal(String docketId, final OnResultListener onResultListener, Type typeOfT) {
        ThreadManager.getInstance().execute(() -> {
            DocketDto docketDto = DBApi.getInstance().queryDocketLogById(docketId);
            ApiResultData<Object> result = new ApiResultData<>();
            result.setStatus(2000);
            result.setMessage(docketDto == null ? DbMsg.EMPTY_DOCKET : DbMsg.QUERY_SUCCESS);
            result.setData(docketDto);
            OKHttpUtil.getInstance().getHandler().post(() -> onResultListener.onSuccess(result));
        });
    }

    public void qcCheck(Object bean, OnResultListener onResultListener, Type typeOfT) {
//        OKHttpUtil.getInstance().postAsync(OKHttpUtil.NET_URL + "/api/dockets/qCCheck",bean, new Callback() {
//            @Override
//            public void onSuccess(Object obj) {
//                onResultListener.onSuccess(obj);
//            }
//
//            @Override
//            public void onFailed(Exception e) {
//                onResultListener.onFails(e);
//            }
//        }, typeOfT);
        ApiResultData<Object> result = new ApiResultData<>();
        result.setStatus(2000);
        result.setMessage(DbMsg.UPLOAD_SUCCESS);
        result.setData(null);
        onResultListener.onSuccess(result);

    }

    public void revertSign(Object bean, OnResultListener onResultListener, Type typeOfT) {
//        OKHttpUtil.getInstance().postAsync(OKHttpUtil.NET_URL + "/api/dockets/driverRevertSign",bean, new Callback() {
//            @Override
//            public void onSuccess(Object obj) {
//                onResultListener.onSuccess(obj);
//            }
//
//            @Override
//            public void onFailed(Exception e) {
//                onResultListener.onFails(e);
//            }
//        }, typeOfT);
        ApiResultData<Object> result = new ApiResultData<>();
        result.setStatus(2000);
        result.setMessage(DbMsg.UPDATE_SUCCESS);
        result.setData(null);
        onResultListener.onSuccess(result);
    }

    public void driverResetSign(Object bean, OnResultListener onResultListener, Type typeOfT) {
//        OKHttpUtil.getInstance().postAsync(OKHttpUtil.NET_URL + "/api/dockets/driverResetSign",bean, new Callback() {
//            @Override
//            public void onSuccess(Object obj) {
//                onResultListener.onSuccess(obj);
//            }
//
//            @Override
//            public void onFailed(Exception e) {
//                onResultListener.onFails(e);
//            }
//        }, typeOfT);
        ApiResultData<Object> result = new ApiResultData<>();
        result.setStatus(2000);
        result.setMessage(DbMsg.RESET_SUCCESS);
        result.setData(null);
        onResultListener.onSuccess(result);
    }

    public void update(Object bean, OnResultListener onResultListener, Type typeOfT) {
//        OKHttpUtil.getInstance().postAsync(OKHttpUtil.NET_URL + "/api/dockets/driverSign",bean, new Callback() {
//            @Override
//            public void onSuccess(Object obj) {
//                onResultListener.onSuccess(obj);
//            }
//
//            @Override
//            public void onFailed(Exception e) {
//                onResultListener.onFails(e);
//            }
//        }, typeOfT);
        ApiResultData<Object> result = new ApiResultData<>();
        result.setStatus(2000);
        result.setMessage(DbMsg.UPDATE_SUCCESS);
        result.setData(null);
        onResultListener.onSuccess(result);
    }

    public void scanPaper(String docketNo, OnResultListener onResultListener, Type typeOfT) {
//        OKHttpUtil.getInstance().getAsync(OKHttpUtil.NET_URL + "/api/dockets/scanPaperRq?docketNo="+docketNo, new Callback() {
//            @Override
//            public void onSuccess(Object obj) {
//                onResultListener.onSuccess(obj);
//            }
//
//            @Override
//            public void onFailed(Exception e) {
//                onResultListener.onFails(e);
//            }
//        }, typeOfT);
        ThreadManager.getInstance().execute(() -> {
            ApiResultData<Object> result = new ApiResultData<>();
            result.setStatus(2000);
            DocketDto docketDto = DBApi.getInstance().queryDocketScanPaper(docketNo);
            Log.i(AllConstants.TAG, GsonUtil.getInstance().toJson(docketDto));
            if (docketDto == null) {
                result.setMessage(DbMsg.EMPTY_DOCKET);
                result.setData(null);
            } else {
                if (docketDto.getState() == 6) {
                    result.setMessage(DbMsg.DOCKET_VOID);
                    result.setData(null);
                } else if (docketDto.getTruckState() != 0 || docketDto.getState() != 0) {
                    result.setMessage(DbMsg.DOCKET_IN_PROGRESS);
                    result.setData(null);
                } else {
                    result.setMessage(DbMsg.QUERY_SUCCESS);
                    result.setData(docketDto);
                }
            }
            OKHttpUtil.getInstance().getHandler().post(() -> onResultListener.onSuccess(result));
        });
    }

    public void confirm(Object bean, OnResultListener onResultListener, Type typeOfT) {
//        OKHttpUtil.getInstance().postAsync(OKHttpUtil.NET_URL + "/api/dockets/confirm",bean, new Callback() {
//            @Override
//            public void onSuccess(Object obj) {
//                onResultListener.onSuccess(obj);
//            }
//
//            @Override
//            public void onFailed(Exception e) {
//                onResultListener.onFails(e);
//            }
//        }, typeOfT);
        ApiResultData<Object> result = new ApiResultData<>();
        result.setStatus(2000);
        result.setMessage(DbMsg.UPLOAD_SUCCESS);
        result.setData(null);
        onResultListener.onSuccess(result);
    }

    public void comWithoutSign(Object bean, OnResultListener onResultListener, Type typeOfT) {
//        OKHttpUtil.getInstance().postAsync(OKHttpUtil.NET_URL + "/api/dockets/comWithoutSign",bean, new Callback() {
//            @Override
//            public void onSuccess(Object obj) {
//                onResultListener.onSuccess(obj);
//            }
//
//            @Override
//            public void onFailed(Exception e) {
//                onResultListener.onFails(e);
//            }
//        }, typeOfT);
        ApiResultData<Object> result = new ApiResultData<>();
        result.setStatus(2000);
        result.setMessage(DbMsg.UPLOAD_SUCCESS);
        result.setData(null);
        onResultListener.onSuccess(result);
    }

    public void additionalCharge(Object bean, OnResultListener onResultListener, Type typeOfT) {
//        OKHttpUtil.getInstance().postAsync(OKHttpUtil.NET_URL + "/api/dockets/additionalCharge",bean, new Callback() {
//            @Override
//            public void onSuccess(Object obj) {
//                onResultListener.onSuccess(obj);
//            }
//
//            @Override
//            public void onFailed(Exception e) {
//                onResultListener.onFails(e);
//            }
//        }, typeOfT);
        ApiResultData<Object> result = new ApiResultData<>();
        result.setStatus(2000);
        result.setMessage(DbMsg.UPLOAD_SUCCESS);
        result.setData(null);
        onResultListener.onSuccess(result);
    }

    public void scanPaper(ScanPaperDto bean, OnResultListener onResultListener) {
        ThreadManager.getInstance().execute(() -> {
            ApiResultData<Object> result = new ApiResultData<>();
            result.setStatus(1000);
            result.setData(null);
            DocketDto docketDto = DBApi.getInstance().queryDocketScanPaper(bean.getDocketNo());
            Log.i(AllConstants.TAG, GsonUtil.getInstance().toJson(docketDto));
            if (docketDto == null) {
                result.setMessage(DbMsg.EMPTY_DOCKET);
            } else {
                if (docketDto.getState() == 6) {
                    result.setMessage(DbMsg.DOCKET_VOID);
                } else if (docketDto.getTruckState() != 0 || docketDto.getState() != 0) {
                    result.setMessage(DbMsg.DOCKET_IN_PROGRESS);
                } else {
                    boolean update = DBApi.getInstance().updateDocketScanPhoto(bean.getDocketNo(), bean.getPhoto());
                    if (update) {
                        result.setStatus(2000);
                        result.setMessage(DbMsg.UPLOAD_SUCCESS);
                    } else {
                        result.setMessage(DbMsg.UPLOAD_FAIL);
                    }
                }
            }
            OKHttpUtil.getInstance().getHandler().post(() -> onResultListener.onSuccess(result));
        });

    }

    public void dockets(Object bean, OnResultListener onResultListener, Type typeOfT) {
//        OKHttpUtil.getInstance().postAsync(OKHttpUtil.NET_URL + "/api/dockets/onHand",bean, new Callback() {
//            @Override
//            public void onSuccess(Object obj) {
//                onResultListener.onSuccess(obj);
//            }
//
//            @Override
//            public void onFailed(Exception e) {
//                onResultListener.onFails(e);
//            }
//        }, typeOfT);
        ThreadManager.getInstance().execute(() -> {
            DocketQuery query = (DocketQuery) bean;
            int count = DBApi.getInstance().queryDocketOnHandCount(query.getTruckNo()); // 总数
            List<DocketDto> list = DBApi.getInstance().queryDocketOnHandList(query.getTruckNo(), StringUtil.getOffset(query.getPageIndex()), StringUtil.getLimit());
            PagedList<DocketDto> pagedList = getDocketList(count, query.getPageIndex(), list);
            ApiResultData<Object> result = new ApiResultData<>();
            result.setStatus(2000);
            result.setMessage(DbMsg.QUERY_SUCCESS);
            result.setData(pagedList);
            OKHttpUtil.getInstance().getHandler().post(() -> onResultListener.onSuccess(result));
        });
    }

    public void docketsLog(Object bean, String what, OnResultListener onResultListener, Type typeOfT) {
        OKHttpUtil.getInstance().postAsync(OKHttpUtil.NET_URL + "/api/dockets/deliveryLogs",bean, new Callback() {
            @Override
            public void onSuccess(Object obj) {
                onResultListener.onSuccess(obj);
            }

            @Override
            public void onFailed(Exception e) {
                docketsLogLocal(bean, what, onResultListener, typeOfT);
//                onResultListener.onFails(e);
            }
        }, typeOfT);
    }

    public void docketsLogLocal(Object bean, String what, OnResultListener onResultListener, Type typeOfT) {
        ThreadManager.getInstance().execute(() -> {
            DocketQuery query = (DocketQuery) bean;
            int count = DBApi.getInstance().queryDocketLogCount(query, what); // 总数
            List<DocketDto> list = DBApi.getInstance().queryDocketLogList(query, StringUtil.getOffset(query.getPageIndex()), StringUtil.getLimit(), what);
            PagedList<DocketDto> pagedList = getDocketList(count, query.getPageIndex(), list);
            ApiResultData<Object> result = new ApiResultData<>();
            result.setStatus(2000);
            result.setMessage(DbMsg.QUERY_SUCCESS);
            result.setData(pagedList);
            OKHttpUtil.getInstance().getHandler().post(() -> onResultListener.onSuccess(result));
        });
    }

    private PagedList<DocketDto> getDocketList(int count, int pageInx, List<DocketDto> list) {
        int totalPages = (count / 10) + 1;
        PagedList<DocketDto> pagedList = new PagedList<>();
        pagedList.setPageIndex(pageInx);
        pagedList.setTotalPages(totalPages);
        pagedList.setList(list);
        pagedList.setTotalCount(count);
        return pagedList;
    }

    public void getSummary(String truckNo, final OnResultListener onResultListener, Type typeOfT) {
        OKHttpUtil.getInstance().getAsync(OKHttpUtil.NET_URL + "/api/dockets/summary?truckNo=" + truckNo, new Callback() {
            @Override
            public void onSuccess(Object obj) {
                onResultListener.onSuccess(obj);
            }

            @Override
            public void onFailed(Exception e) {
                getSummaryLocal(truckNo, onResultListener, typeOfT);
//                onResultListener.onFails(e);
            }
        }, typeOfT);
    }

    public void getSummaryLocal(String truckNo, final OnResultListener onResultListener, Type typeOfT) {
        ThreadManager.getInstance().execute(() -> {
            int today = DBApi.getInstance().queryDocketTodayCount(truckNo);
            int month = DBApi.getInstance().queryDocketMonthCount(truckNo);
            DocketSummaryDto bean = new DocketSummaryDto();
            bean.setTodayComQty(today);
            bean.setMonthComQty(month);
            ApiResultData<Object> result = new ApiResultData<>();
            result.setStatus(2000);
            result.setMessage(DbMsg.QUERY_SUCCESS);
            result.setData(bean);
            OKHttpUtil.getInstance().getHandler().post(() -> onResultListener.onSuccess(result));
        });
    }

    public void addInfo(Object bean, OnResultListener onResultListener, Type typeOfT) {
//        OKHttpUtil.getInstance().postAsync(OKHttpUtil.NET_URL + "/api/dockets/addAca", bean, new Callback() {
//            @Override
//            public void onSuccess(Object obj) {
//                onResultListener.onSuccess(obj);
//            }
//
//            @Override
//            public void onFailed(Exception e) {
//                onResultListener.onFails(e);
//            }
//        }, typeOfT);
        ApiResultData<Object> result = new ApiResultData<>();
        result.setStatus(2000);
        result.setMessage(DbMsg.UPDATE_SUCCESS);
        result.setData(null);
        onResultListener.onSuccess(result);
    }

    public void addAccRep(Object bean, OnResultListener onResultListener, Type typeOfT) {
//        OKHttpUtil.getInstance().postAsync(OKHttpUtil.NET_URL + "/api/dockets/addAccRep", bean, new Callback() {
//            @Override
//            public void onSuccess(Object obj) {
//                onResultListener.onSuccess(obj);
//            }
//
//            @Override
//            public void onFailed(Exception e) {
//                onResultListener.onFails(e);
//            }
//        }, typeOfT);
        ApiResultData<Object> result = new ApiResultData<>();
        result.setStatus(2000);
        result.setMessage(DbMsg.UPDATE_SUCCESS);
        result.setData(null);
        onResultListener.onSuccess(result);
    }
}
